﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Controls.HtmlControls;
using SM2Utilities;
using SM2Profile;

namespace SM2SetupSearch
{
    public class Search
    {
        public static void AddQuickSearch(string strProfile, string strKeyword)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }

            //Input keyword
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
            HtmlInputText cphPageContent_tbQuickSearch = cphPageContent_pnlSearchSetup.Find.ById<HtmlInputText>("cphPageContent_tbQuickSearch");
            SM2Input.Tradition.TypeTextIntoTextFieldWithHint(cphPageContent_tbQuickSearch, strKeyword);

            //Hit Add
            SearchCommonActions.ClikOnButton("Add");

            //Verify it was created
            string strID = TableOfSearchActions.GetIDKeyword(strKeyword);
            if (!strID.Equals("")) Utilities.GlobalObject.LOGGER.log("New Search was created successfully [" + strID + "]");
            else throw new Exception("Fail to create Search with Keyword = [" + strKeyword + "]");
        }

        public static void SetEnableSearch(string strProfile, string strKeyword)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }

            string strCurrentStatus = TableOfSearchActions.GetStatus(strKeyword);
            if (strCurrentStatus.ToLower().Equals("disabled"))
            {
                //Check true for checkbox
                HtmlInputCheckBox checkbox = TableOfSearchActions.GetCheckBox(strKeyword);
                checkbox.Checked = true;

                //Hit Enable button
                SearchCommonActions.ClikOnButton("Enable");
            }
        }

        public static void SetDisableSearch(string strProfile, string strKeyword)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }

            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            string strCurrentStatus = TableOfSearchActions.GetStatus(strKeyword);
            if (strCurrentStatus.ToLower().Equals("enabled"))
            {
                //Check true for checkbox
                HtmlInputCheckBox checkbox = TableOfSearchActions.GetCheckBox(strKeyword);
                checkbox.Checked = true;

                //Hit Enable button
                SearchCommonActions.ClikOnButton("Disable");
            }
        }

        public static void EditSearch(string strProfile, string strKeyword, string strNewKeyword)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }

            string strIDKeyword = TableOfSearchActions.GetIDKeyword(strKeyword);
            if (!strIDKeyword.Equals(""))
            {
                //Check true for checkbox
                HtmlAnchor anchor = TableOfSearchActions.GetEditAnchor(strKeyword);
                SM2Input.Tradition.ClickAnchor(anchor);

                //Update keyword
                QueryBuilderActions.QueryBuilder_SetTextInto_builder_all_these_words_box(strNewKeyword);

                //Hit Save
                SearchCommonActions.SaveInSearchSettingPage();
            }
        }

        public static void DeleteSearch(string strProfile, string strKeyword)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }

            string strIDKeyword = TableOfSearchActions.GetIDKeyword(strKeyword);
            if (!strIDKeyword.Equals(""))
            {
                //Check true for checkbox
                HtmlInputCheckBox checkbox = TableOfSearchActions.GetCheckBox(strKeyword);
                checkbox.Checked = true;

                //Hit Enable button
                SearchCommonActions.ClikOnButton("Delete");

                //Handle confirm
                SearchCommonActions.ConfirmDelete(true, true);
            }
        }

        public static void RunHistorical(string strProfile, string strStartDate, string strEndDate, string strConfirm)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }

            //Hit Run Historical
            SearchCommonActions.ClikOnButton("Run Historical");

            //Set date
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlMpeRunHistorical = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlMpeRunHistorical");
            HtmlInputText cphPageContent_tbDateFromRunHistorical = cphPageContent_pnlMpeRunHistorical.Find.ById<HtmlInputText>("cphPageContent_tbDateFromRunHistorical");
            SM2Input.Tradition.TypeTextIntoTextFieldWithHint(cphPageContent_tbDateFromRunHistorical, strStartDate);

            HtmlInputText cphPageContent_tbDateToRunHistorical = cphPageContent_pnlMpeRunHistorical.Find.ById<HtmlInputText>("cphPageContent_tbDateToRunHistorical");
            SM2Input.Tradition.TypeTextIntoTextFieldWithHint(cphPageContent_tbDateToRunHistorical, strEndDate);
                
            //Final action Apply or Cancel
            if (strConfirm.Equals("yes"))
            {
                HtmlInputSubmit cphPageContent_btnRunHistoricalOk = cphPageContent_pnlMpeRunHistorical.Find.ById<HtmlInputSubmit>("cphPageContent_btnRunHistoricalOk");
                SM2Input.Tradition.ClickButton(cphPageContent_btnRunHistoricalOk);
            }
            else
            {
                HtmlInputSubmit cphPageContent_btnRunHistoricalCancel = cphPageContent_pnlMpeRunHistorical.Find.ById<HtmlInputSubmit>("cphPageContent_btnRunHistoricalCancel");
                SM2Input.Tradition.ClickButton(cphPageContent_btnRunHistoricalCancel);
            }
        }

        public static void GlobalExcludes(string strProfile, string strExcludedWords)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }

            //Hit Global Excludes
            SearchCommonActions.ClikOnButton("Global Excludes");

            //Add
            GlobalExcludeActions.AddPhrase(strExcludedWords);

            //Apply
            GlobalExcludeActions.Confirm("yes");
        }
        
        public static void NewSearchManualEntry(string strProfile, string strKeyword)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }
            if (!SearchCommonActions.wasExisted(strProfile, strKeyword))
            {
                //Hit New Search
                SearchCommonActions.ClikOnButton("New Search");
                System.Threading.Thread.Sleep(5000);

                //Switch
                SearchCommonActions.ChangeTypeOfSeachCreation(SetupSearch.MANUALENTRY);

                //Input keyword
                ManualEntryActions.ManualEntry_SetTextInto_search_text(strKeyword);

                SearchCommonActions.FilterResultsForSpam(true);
                SearchCommonActions.EnabledKeywordUponCreating(true);
                //SearchCommonActions.FilterByCountryInclude("United States");
                //SearchCommonActions.FilterByLanguageExclude("English");
                SearchCommonActions.FilterByDateRange("06/13/2011", "06/15/2011");
                //SearchCommonActions.AdvancedExcludes("this is for excluding");
                //SearchCommonActions.OrganizeResults("Competitor Product", "ForDemo");

                //Save
                SearchCommonActions.SaveInSearchSettingPage();
                System.Threading.Thread.Sleep(5000);

                //Verify it was created
                string strID = TableOfSearchActions.GetIDKeyword(strKeyword);
                if (!strID.Equals("")) Utilities.GlobalObject.LOGGER.log("New Search was created successfully [" + strID + "]");
                else throw new Exception("Fail to create Search with Keyword = [" + strKeyword + "]");

                //Validate it was run completely
                CommonLoader.load("jobs.aspx");
                SM2SetupJobs.RunningCompleted.checkSearchCompleted(strKeyword);

                //Check Result to make sure we get some - then disable it
                CommonLoader.load("scan_results.aspx");
                int intFoundNumberResults = SM2ViewResults.ViewResultsActions.CountNumberOfResult(strKeyword);
            }
        }

        public static void NewSearchQueryBuilder(string strProfile, string strKeyword)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }

            if (!SearchCommonActions.wasExisted(strProfile, strKeyword))
            {

                //Hit New Search
                SearchCommonActions.ClikOnButton("New Search");

                //Switch
                SearchCommonActions.ChangeTypeOfSeachCreation(SetupSearch.QUERYBUILDER);

                //Input keyword
                QueryBuilderActions.QueryBuilder_SetTextInto_builder_all_these_words_box(strKeyword);

                SearchCommonActions.FilterResultsForSpam(false);
                SearchCommonActions.EnabledKeywordUponCreating(false);
                SearchCommonActions.FilterByCountryInclude("Bosnia And Herzegovina");
                SearchCommonActions.FilterByLanguageExclude("English");
                SearchCommonActions.FilterByDateRange("05/13/2011", "06/13/2011");
                SearchCommonActions.AdvancedExcludes("this is for excluding");
                SearchCommonActions.OrganizeResults("Competitor Product", "ForDemo");

                //Save
                SearchCommonActions.SaveInSearchSettingPage();

                //Verify it was created
                string strID = TableOfSearchActions.GetIDKeyword(strKeyword);
                if (!strID.Equals("")) Utilities.GlobalObject.LOGGER.log("New Search was created successfully [" + strID + "]");
                else throw new Exception("Fail to create Search with Keyword = [" + strKeyword + "]");
            }
        }

        public static void NewSearchBulkUpload(string strProfile, string strKeyword)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("advanced_search_keywords.aspx");
            }

            //Hit New Search
            SearchCommonActions.ClikOnButton("New Search");

            //Switch
            SearchCommonActions.ChangeTypeOfSeachCreation(SetupSearch.BULKUPLOAD);

            //Input keyword
            BulkUploadActions.BulkUpload_SetTextInto_bulk_search_text(strKeyword);

            SearchCommonActions.FilterResultsForSpam(false);
            SearchCommonActions.EnabledKeywordUponCreating(false);
            SearchCommonActions.FilterByCountryInclude("Bosnia And Herzegovina");
            SearchCommonActions.FilterByLanguageExclude("English");
            SearchCommonActions.FilterByDateRange("05/13/2011", "06/13/2011");
            SearchCommonActions.AdvancedExcludes("this is for excluding");
            SearchCommonActions.OrganizeResults("Competitor Product", "ForDemo");

            //Save
            SearchCommonActions.SaveInSearchSettingPage();

            //Verify it was created
            string strID = TableOfSearchActions.GetIDKeyword(strKeyword);
            if (!strID.Equals("")) Utilities.GlobalObject.LOGGER.log("New Search was created successfully [" + strID + "]");
            else throw new Exception("Fail to create Search with Keyword = [" + strKeyword + "]");
        }
    }
}
